<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Lianggoods;
use App\Admin\Categoods;
use App\Admin\GetCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class LianggoodsController extends Controller
{
    //商品品列表显示
    public function lianggoodsshow(Request $request){
//        dd($request);
        //将分类数据库表遍历到后台
        if($request->search){
            $search = $request->search;
            $searchlist =Lianggoods::where('name','like','%'.$search.'%');
            $result=$searchlist->paginate(5);
        }else{
            $Sing = Lianggoods::where([])->orderBy('categoods_id','asc');
            $result = $Sing->paginate(5);
        }
        return view('admin/lianggoods/LianggoodsList',compact('result'));
    }
    //添加商品
    public function lianggoodsadd(){
        $Cate = Categoods::all();
        //new getTree方法
        $getCate = new GetCategory();
//        var_dump($getCate);
        $CateList= $getCate->getTree($Cate);

        return view('admin/lianggoods/LianggoodsAdd',['CateList'=>$CateList]);
    }
    public function lianggoodsadddata(Request $request){
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
            'describe'=>'required|between:5,150',
            'price'=>'integer|alpha_num',
            'pic'=>'required',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '商品名称不能为空',
            'name.between'=>'商品名称必须在:min和:max之间',
            'describe.required' => '商品描述不能为空',
            'describe.between'=>'商品描述必须在:min和:max之间',
            'price.integer' => '商品价格格式不对',
            'price.alpha_num'=>'商品价格不能为负数',
            'pic.required'=>'商品图片不能为空',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/LianggoodsAdd')->withErrors($validator);
        }
        $input = $request->all();
//        dd($input);
        $input['time']=time();
        //把传来的图片信息更改上传public/adminliang/
        $input['pic']=time().'.jpg';
        $pic = $input['pic'];

        $request->pic->move(public_path() . '/adminliang', $pic);
        $res = Lianggoods::create($input);
//        dd($res);
        return redirect('admin/LianggoodsList');
    }
    //修改单品
    public function lianggoodsedit(){
        //见分类数据库表遍历到后台
//        dd($_GET);

        $search = isset($_GET['search'])?$_GET['search']:'';
        $page = $_GET['page'];
        $id = $_GET['id'];
        $Lianggoods = Lianggoods::where('id','=',$id)->get();
        $Liang = Categoods::all();
//        $Sing['page']=$page;
        $getLiang = new GetCategory();
        $LiangEdit= $getLiang->getTree($Liang);
        return view('admin/lianggoods/LianggoodsEdit',['Lianggoods'=>$Lianggoods,'LiangEdit'=>$LiangEdit,'page'=>$page,'search'=>$search]);
    }
    public function lianggoodseditdata(Request $request,$page,$search = ''){
        //接收要修改的ID
        $id = $request->id;
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
            'describe'=>'required|between:5,150',
            'price'=>'integer|alpha_num',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '商品名称不能为空',
            'name.between'=>'商品名称必须在:min和:max之间',
            'describe.required' => '商品描述不能为空',
            'describe.between'=>'商品描述必须在:min和:max之间',
            'price.integer' => '商品价格格式不对',
            'price.alpha_num'=>'商品价格不能为负数',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/LianggoodsEdit?id='.$id.'&page='.$page.'&search'.$search)->withErrors($validator);
        }else{
//            dd($search);
            if($request->hasFile('pic')){
                $xiu = Lianggoods::find($id);
                $ppic=$xiu['pic'];
//                dd($ppic);
                @unlink(public_path().'/adminliang'.'/'.$ppic);
                $inpu=$request->all();
                $inpu['pic']=time().'.jpg';
                $pic = $inpu['pic'];

                $request->pic->move(public_path().'/adminliang', $pic);
                $input = Input::except('_token');
                $input['pic']=$pic;
//                dump($id);
//                dd($input);
                $res = Lianggoods::where('id',$id)->update($input);
                return redirect('admin/LianggoodsList?page='.$page.'&search='.$search);
            }else{
                $input = Input::except('_token');
                $res = Lianggoods::where('id',$id)->update($input);
                return redirect('admin/LianggoodsList?page='.$page.'&search='.$search);
            }
        }
    }
//    删除单品
    public function lianggoodsdel(Request $request){
        $search=$request->search;
        $id = $_GET['id'];
        $page=$_GET['page'];
        $Lianggoods = Lianggoods::find($id);
        $ppic=$Lianggoods['pic'];
        @unlink(public_path().'/adminliang'.'/'.$ppic);
        $LianggoodsList = $Lianggoods->delete();
        if($search==''){
            $count = DB::table('lianggoods')->count();
//            dd($count);
        }else{
            $count = DB::table('lianggoods')
                    ->where('name','like','%'.$search.'%')
                    ->count();
        }
        $num = $count%5;
        if($num==0){
            $page= $page-1;
        }
        return redirect('admin/LianggoodsList?page='.$page.'&search='.$search);
    }
}
